<script type="text/javascript">
$(function(){
	var pg = App.codex.beanMappingEdit.ui,
		pp = pg.params || {};
	
	var frm = {
		toObj: pg.seek('toObj'),
		toType: pg.seek('toType'),
		fromObj: pg.seek('fromObj'),
		fromType: pg.seek('fromType'),
		toProp: pg.seek('toProp'),
		toPropType: pg.seek('toPropType'),
		fromProp: pg.seek('fromProp'),
		fromPropType: pg.seek('fromPropType')
	};
	
	var toObjGd = $('<div class="grid"></div>').mac('grid', {
		keyField: 'path',
		cols : [{
			field: 'path', width: 320, title: '路径'
		}, {
			field: 'name', width: 120, title: '属性名'
		}, {
			field: 'type', width: 320, title: '类型'
		}],
		loader: {
			url: App.ctx + '/bean/prop/list.do',
			params: {
				pageNo: 1, pageSize: 50,
				beanType: pp.toType,
				recursively: true
			},
			autoLoad: true
		},
		pagerLength: 10
	});
	frm.toObj.mac('combo', {
		keyField: 'id',
		displayField: 'path',
		multiSelect: false,
		allowInput: true,
		width: 506,
		boxLeft: 0,
		boxWidth: 500,
		boxHeight: 320,
		boxEl: toObjGd,
		onSelect: function(me, sk, sd){
			var r = (sd[0] || {});
			if (r.id) {
				pp.toObj = r.path + '.' + r.name;
				frm.toType.text(r.type);
				toPropGd.load({ beanType: r.type });
			}
		}
	}).mac('combo_grid', {}).val(pp.toObj);
	
	var toPropGd = $('<div class="grid"></div>').mac('grid', {
		cols : [{
			field: 'path', width: 320, title: '路径'
		}, {
			field: 'name', width: 120, title: '属性'
		}, {
			field: 'type', width: 320, title: '类型'
		}],
		loader: {
			url: App.ctx + '/bean/prop/list.do',
			params: {
				pageNo: 1, pageSize: 50,
				beanType: pp.toType,
				recursively: false
			},
			autoLoad: true
		},
		pagerLength: 10
	});
	frm.toProp.mac('combo', {
		keyField: 'id',
		displayField: 'name',
		multiSelect: false,
		allowInput: true,
		width: 506,
		boxLeft: 0,
		boxWidth: 500,
		boxHeight: 320,
		boxEl: toPropGd,
		onSelect: function(me, sk, sd){
			var r = (sd[0] || {});
			if (r.id) {
				pp.toProp = r.name;
				pp.toPropType = r.type;
				frm.toPropType.text(r.type);
			}
		}
	}).mac('combo_grid', {}).val(pp.toProp);
	
	var fromPropGd = $('<div class="grid"></div>').mac('grid', {
		cols : [{
			field: 'path', width: 320, title: '路径'
		}, {
			field: 'name', width: 120, title: '属性'
		}, {
			field: 'type', width: 320, title: '类型'
		}],
		loader: {
			url: App.ctx + '/bean/prop/list.do',
			params: {
				pageNo: 1, pageSize: 50,
				beanType: pp.fromType,
				recursively: false
			},
			autoLoad: true
		},
		pagerLength: 10
	});
	frm.fromProp.mac('combo', {
		keyField: 'id',
		displayField: 'name',
		multiSelect: false,
		allowInput: true,
		width: 506,
		boxLeft: 0,
		boxWidth: 500,
		boxHeight: 320,
		boxEl: fromPropGd,
		onSelect: function(me, sk, sd){
			var r = (sd[0] || {});
			if (r.id) {
				pp.fromProp = r.name;
				pp.fromPropType = r.type;
				frm.fromPropType.text(r.type);
			}
		}
	}).mac('combo_grid', {}).val(pp.fromProp);

	frm.toObj.val(pp.toObj);
	frm.toType.text(pp.toType);
	frm.fromObj.val(pp.fromObj);
	frm.fromType.text(pp.fromType);
	frm.toProp.find('.head input').val(pp.toProp);
// 	frm.toProp.val(pp.toProp);
	frm.toPropType.text(pp.toPropType);
	frm.fromProp.find('.head input').val(pp.fromProp);
// 	frm.fromProp.val(pp.fromProp);
	frm.fromPropType.text(pp.fromPropType);

	function getValues(){
		return $.extend(pp, {
			toObj: frm.toObj.txt(),
			toType: frm.toType.txt(),
			toProp: frm.toProp.txt(),
			toPropType: frm.toPropType.txt(),
			fromObj: frm.fromObj.val(),
			fromType: frm.fromType.val(),
			fromProp: frm.fromProp.txt(),
			fromPropType: frm.fromPropType.val()
		});
	}

	function validate(p){
// 		var x2 = App.validate2, v = App.code.validation,
// 			m = pg.find('.msg').html('');
// 		return x2(v.svc.code, frm.code)
// 			& x2(v.svc.name, frm.name);
		return true;
	}
	pg.seek('ok').click(function(){
		var pms = getValues();
		if (!validate(pms)) {
			return;
		}
		$.post(App.codex.ctx + '/bean/mapping/save.do', pms, function(rs){
			var ro = mac.eval(rs);
			if (ro.success) {
				pg.callback(ro.data);
				if (pg.close) {
					pg.close();
				} else {
					pg.callback(null, pp.id || pp.parent);
				}
			} else {
				pg.seek('msg').html(mac.msg(ro.data, ro.params));
			}
		});
		return false;
	});
});
</script>
<div class="form">
	<div class="item">
		<span class="label">目标对象</span>
		<div class="combo fl" name="toObj"></div>
		<div class="clear"></div>
	</div>
	<div class="item">
		<span class="label">目标对象类型</span>
		<div class="combo fl" name="toType"></div>
		<div class="clear"></div>
	</div>
	<div class="item">
		<span class="label">目标属性</span>
		<div class="combo fl" name="toProp"></div>
		<div class="clear"></div>
	</div>
	<div class="item">
		<span class="label">目标属性类型</span>
		<div class="combo fl" name="toPropType"></div>
		<div class="clear"></div>
	</div>
	<div class="item">
		<span class="label">来源对象</span>
		<div class="combo fl" name="fromObj"></div>
		<div class="clear"></div>
	</div>
	<div class="item">
		<span class="label">来源对象类型</span>
		<div class="combo fl" name="fromType"></div>
		<div class="clear"></div>
	</div>
	<div class="item">
		<span class="label">来源属性</span>
		<div class="combo fl" name="fromProp"></div>
		<div class="clear"></div>
	</div>
	<div class="item">
		<span class="label">来源属性类型</span>
		<div class="combo fl" name="fromPropType"></div>
		<div class="clear"></div>
	</div>
	<div class="item">
		<span class="label">&nbsp;</span>
		<div name="msg"></div>
		<div class="clear"></div>
	</div>
	<div class="bottom">
		<button name="ok" class="fr">保存</button>
	</div>
</div>